package com.ardf.controller;

import com.ardf.entity.dto.CardDto;
import com.ardf.entity.dto.CardSearchAllDto;
import com.ardf.entity.dto.ReplaceCardTableDto;
import com.ardf.entity.dto.UnitMenuDto;
import com.ardf.service.ICardService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/card")
public class CardController {
    private final ICardService cardService;

    public CardController(ICardService cardService) {
        this.cardService = cardService;
    }

    /**
     * 获取单位目录
     */
    @GetMapping("/getMenu")
    public ResponseEntity<List<UnitMenuDto>> getMenu(@RequestParam("competitionId") Long competitionId) {
        List<UnitMenuDto> unitMenuDtos = cardService.getMenu(competitionId);
        return ResponseEntity.ok(unitMenuDtos);
    }

    /**
     * 制卡分页
     */
    @GetMapping("/searchAll")
    public ResponseEntity<Page<CardSearchAllDto>> searchAll(@RequestParam("pageSize") Integer pageSize,
                                                            @RequestParam("pageNum") Integer pageNum,
                                                            @RequestParam("unitId") Long unitId,
                                                            @RequestParam(value = "searchAll", required = false) String searchAll) {
        Page<CardSearchAllDto> page;
        if (searchAll!=null&&StringUtils.hasLength(searchAll.trim())) {
            page = cardService.searchAll(pageSize, pageNum, unitId, searchAll.trim());
        } else {
            page = cardService.searchAll(pageSize, pageNum, unitId, null);
        }
        return ResponseEntity.ok(page);
    }

    /**
     * 补卡分页
     */
    @GetMapping("/replaceCardSearchAll")
    public ResponseEntity<Page<ReplaceCardTableDto>> replaceCardSearchAll(@RequestParam("pageSize") Integer pageSize,
                                                                          @RequestParam("pageNum") Integer pageNum,
                                                                          @RequestParam("competitionId") Long competitionId,
                                                                          @RequestParam(value = "searchAll", required = false) String searchAll) {
        Page<ReplaceCardTableDto> page;
        if (searchAll!=null&&StringUtils.hasLength(searchAll.trim())) {
            page = cardService.replaceCardSearchAll(pageSize, pageNum, competitionId, searchAll.trim());

            for(ReplaceCardTableDto dto:page.getRecords()){
                //根据选手Id查询所有的补卡记录
                List<CardDto> cardDtos = cardService.selectReplaceCardByAthId(dto.getAthleteId());
                System.out.println(cardDtos);
                if(cardDtos.size()>0){
                    dto.setNewCardNum(cardDtos.get(cardDtos.size()-1).getNumber());
                }
                for (int i = cardDtos.size()-2; i >=0; i--) {
                    dto.setOldCardNum(dto.getOldCardNum()+","+cardDtos.get(i).getNumber());
                }
            }
        } else {
            page = cardService.replaceCardSearchAll(pageSize, pageNum, competitionId, null);
            for(ReplaceCardTableDto dto:page.getRecords()){
                //根据选手Id查询所有的补卡记录
                List<CardDto> cardDtos = cardService.selectReplaceCardByAthId(dto.getAthleteId());
                System.out.println(cardDtos);
                if(cardDtos.size()>0){
                    dto.setNewCardNum(cardDtos.get(cardDtos.size()-1).getNumber());
                }
                for (int i = cardDtos.size()-2; i >=0; i--) {
                    dto.setOldCardNum(dto.getOldCardNum()+","+cardDtos.get(i).getNumber());
                }
            }
        }
        return ResponseEntity.ok(page);
    }


}
